/*
 * This is a wrapper for leaflet.js that let's us
 * easily set up and perform relevant operations
 * on the leaflet map without calling leaflet directly
 */

/* 
 * Ctor for map
 * mapid is the tag-id of where to put a map
 * latlngs is the array of latitudes and longitudes to plot on the map
 */
var CormacMap = function(mapid,latlngs) {
    this.latlngs = latlngs;
    this.icon = L.icon({
        iconUrl: '/webappz-gu/images/marker-icon.png',
        iconAnchor:   [25, 20]
    });
    this.map = L.map(mapid).setView(this.latlngs[0], 13);
    this.circle = L.circleMarker(this.latlngs[0],0).addTo(this.map);
    
    this.polyline = L.polyline(this.latlngs).addTo(this.map);
    var markerStart = L.marker(this.latlngs[0], {icon: this.icon}).addTo(this.map).bindPopup("Start");
    var markerFin = L.marker(this.latlngs[this.latlngs.length - 1], {icon: this.icon}).addTo(this.map).bindPopup("Finish");
    L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
        attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a>, Imagery <a href="http://mapnik.org/">Mapnik</a>',
        maxZoom: 18
    }).addTo(this.map);
    //this.map.fitBounds(this.polyline.getBounds());
};

CormacMap.prototype = (function() {
    return {
        /*
         * Set a position based on coordinates from a frame
         */
        setFramePos: function(lat, lng) {
            var latlng = new L.LatLng(lat,lng);
            this.map.removeLayer(this.circle);
            this.circle = L.circleMarker(latlng, 10, {color: '#f03', fillcolor: '#f03'}).addTo(this.map);
        },
        /*
         * Set a maxspeed-position based on coordinates from a frame
         */
        setMaxSpeed: function(coord, speed) {
            var latlng = new L.LatLng(coord[0], coord[1]);
            this.markerMaxSpeed = L.marker(latlng, {icon: this.icon}).addTo(this.map).bindPopup("Highest speed: " + speed);
        },
        /*
         * Set a maxpulse-position based on coordinates from a frame
         */
        setMaxPulse: function(coord, pulse) {
            var latlng = new L.LatLng(coord[0], coord[1]);
            this.markerMaxPulse = L.marker(latlng, {icon: this.icon}).addTo(this.map).bindPopup("Highest pulse: " + pulse);
        },
        /*
         * Set a maxaltitude-position based on coordinates from a frame
         */
        setMaxAltitude: function(coord, alt) {
            var latlng = new L.LatLng(coord[0], coord[1]);
            this.markerMaxAltitude = L.marker(latlng, {icon: this.icon}).addTo(this.map).bindPopup("Highest altitude: " + alt);
        }
    };
}());

